package com.itheima.task.timer;

import com.itheima.swagger.Config.FastDFSClientUtil;
import com.itheima.swagger.contants.RedisConstants;
import com.itheima.task.mapper.QuestionMapper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Set;

@Component
@Slf4j
public class ClearPicTask {
    // 秒    分   小时    日   月   周
    //0-59  0-59 0-23  1-31 1-12 1-7
    /*
        特殊字符：
        *：任意
        ?：弃用
        -：0-5  0、1、2、3、4、5
        ,：0、8、23、54
        /：0/5  0 5 10 15 20 25 30 35 40 45...
     */
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private FastDFSClientUtil fastDFSClientUtil;
    @Value("${fileServerUrl}")
    private String filePath;
    @Autowired
    private QuestionMapper questionMapper;

    //@Scheduled(cron = "0/5 * * * * ?")
    @XxlJob("clearPicture")
    public void clearPic(){
        //intersect 交集 union 并集 difference 差集
        Set<String> pics=redisTemplate.opsForSet().difference(RedisConstants.ALL_PIC,RedisConstants.USE_PIC);
        log.info("定时任务开始执行,需要清理的图片有:"+pics);
        for(String pic:pics){
            fastDFSClientUtil.delFile(pic);
            redisTemplate.opsForSet().members(RedisConstants.ALL_PIC).remove(pic);
            questionMapper.deleteById(pic);
        }
    }
}